package com.xyw.code.bus.center.event;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rabbitmq.client.Channel;
import com.xyw.code.bus.center.dao.SysBusMapper;
import com.xyw.code.bus.common.entity.po.SysBus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xuyiwei
 * Date: 2020/2/18
 * Time: 下午9:39
 * Email: 1328312923@qq.com
 * Description: No Description
 **/
@Component
@Slf4j
public class MQErrorReceiver {

    public static final String exchange = "BOOT-EXCHANGE-EROOR";
    public static final String routekey = "error-msg";

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value = "boot.error.queue", durable = "true"),
                    exchange = @Exchange(value = MQErrorReceiver.exchange, type = "topic", durable = "true", ignoreDeclarationExceptions = "true"),
                    key = MQErrorReceiver.routekey
            )
    )
    @RabbitHandler
    public void dealError(Message message, Channel channel) throws Exception {
        //接收到错误专门处理的消息队列  如果这个错误是第一次出现的话  就重新发送这个消息   如果这个错误是第二次出现的话  就直接抛弃
        String correlationId = message.getMessageProperties().getCorrelationId();
        //去数据库查询这个correlationId在错误列表中出现过  如果消费过  并且错误次数已经达到3次了 则直接抛弃  如果还没消费过 则继续发送该消息
        log.info("接收到消息:{}",correlationId);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

}